﻿using JTActiveSafety.Gateway.Configurations;
using JTActiveSafety.Gateway.Sessions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JTActiveSafety.Gateway.Jobs
{
    //internal class JTActiveSafetyTcpReceiveTimeoutJob : BackgroundService
    //{
    //    private readonly ILogger Logger;

    //    private readonly JTActiveSafetySessionManager SessionManager;

    //    private readonly IOptionsMonitor<JTActiveSafetyConfiguration> Configuration;
    //    public JTActiveSafetyTcpReceiveTimeoutJob(
    //            IOptionsMonitor<JTActiveSafetyConfiguration> jT1078ConfigurationAccessor,
    //            ILoggerFactory loggerFactory,
    //            JTActiveSafetySessionManager jT1078SessionManager
    //        )
    //    {
    //        SessionManager = jT1078SessionManager;
    //        Logger = loggerFactory.CreateLogger<JTActiveSafetyTcpReceiveTimeoutJob>();
    //        Configuration = jT1078ConfigurationAccessor;
    //    }

    //    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    //    {
    //        while (!stoppingToken.IsCancellationRequested)
    //        {
    //            try
    //            {
    //                foreach (var item in SessionManager.GetTcpAll())
    //                {
    //                    if (item.LastActiveTime.AddSeconds(Configuration.CurrentValue.TcpReaderIdleTimeSeconds) < DateTime.Now)
    //                    {
    //                        item.ReceiveTimeout.Cancel();
    //                    }
    //                }
    //                if (Logger.IsEnabled(LogLevel.Information))
    //                {
    //                    Logger.LogInformation($"[Check Receive Timeout]");
    //                    Logger.LogInformation($"[Session Online Count]:{SessionManager.TcpSessionCount}");
    //                }
    //            }
    //            catch (Exception ex)
    //            {
    //                Logger.LogError(ex, $"[Receive Timeout]");
    //            }
    //            finally
    //            {
    //                await Task.Delay(TimeSpan.FromSeconds(Configuration.CurrentValue.TcpReceiveTimeoutCheckTimeSeconds), stoppingToken);
    //            }
    //        }
    //    }
    //}
}
